<template>
  <div>
    <BasicForm
      v-if="showSearchForm"
      ref="formData"
      formItemWidth="100px"
      :formColumns="formItems"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    />
    <el-card class="sourcing-table-card" shadow="never">
      <el-row slot="header" type="flex" justify="space-between" align="middle">
        <span>数据列表</span>
        <el-button
          size="mini"
          :loading="exportLoading"
          @click="exportData"
        >导出</el-button>
      </el-row>
      <div class="text item">
        <BasicList
          :show-select-col="false"
          :showActionCol="false"
          :loading="tableInfo.loading"
          :data-source="tableInfo.list"
          :columns="tableInfo.columns"
          :tableHeight="500"
          :total="total"
          :pagination="pagination"
          @pageChange="handlePageChange"
          @pageSizeSelect="handlePageSizeChange"
        />
      </div>
    </el-card>
  </div>
</template>
<script>
import BasicList from '@sourcing/components/BasicList';
import BasicForm from '@/pages/sourcing/components/BasicForm'
import BasicPage from '@/mixins/sourcing/basicPage'
import { createExcelByExport } from '@/utils/bizUtils';
import {
  TABLE_COLUMNS,
  initFormItems
} from './constants'
import {
  queryAreasService,
} from '@/api/sourcing/public'
import {
  queryBidRecordList,
  exportOrderRecordService
} from '@/api/sourcing/purchaseInquiry'

export default {
  name: 'BidRecord',
  mixins: [BasicPage],
  components: {
    BasicForm,
    BasicList,
  },
  data () {
    return {
      showSearchForm: false,
      formItems: [],
      tableInfo: {
        columns: TABLE_COLUMNS,
        list: [],
        loading: false
      },
      formData: {
        billType: '', // 询价类型 1客户询价 2订货询价 3 自进货询价
        purchaseInquirerCompanyName: '', // 采购询价部门
        purchaseInquirerName: '', // 询价人名称
        skuCode: '', // 商品代码
        skuName: '', // 商品名称
        sourceBillNo: '', // 询价单号
        supplierCode: '', // 供应商代码
        supplierName: '', // 供应商名称
        winBidding: '', // 是否定标 1 是 0否
        bidingDate: [], // 定标日期
        inquiryDate: [], // 询价日期
        serviceProviderName: '', // 指定渠道名称
        isProcedures: '', // 是否走手续
        isServiceProvider: '', // 是否服务商
        stockAttr: '', // 存量属性
        signContract: '', // 是否签合同
        categoryCode: '', // 分类
        validityDate: [], // 价格有效期
        isPriceExpireTime: '', // 是否价格有效期内
        priceLowest: '', // 是否价格最低
        saleCompanyId: '', // 销售公司
        saleAreaId: '', // 销售区域
        skuAreaList: [], // 区域商品集合
        skuCodeList: [], // 供应商报价详情商品
        source: 0, // 0 寻源 1 商品中心 2 报价详情
        isSynergy: null,
        supplierClassify: null,
      },
    };
  },
  computed: {
    // 来源 1 潜在商品数据分析 2 供应商报价详情
    source () {
      return this.$route.query.source || 0
    },
  },
  created () {
    this.queryAreaList()
    const { billType, source } = this.$route.query
    if (source == 1) {
      this.formData.source = source
      this.formData.billType = '2'
      this.formData.isProcedures = 40320 // 否
      this.formData.skuAreaList = JSON.parse(localStorage.getItem('product_record_skus')) || []
      this.getTableData()
    } else if (source == 2) {
      this.formData.billType = billType
      this.formData.source = source
      this.formData.skuCodeList = JSON.parse(localStorage.getItem('sourcing_record_skus')) || []
      this.getTableData()
    } else {
      localStorage.removeItem('product_record_skus')
      localStorage.removeItem('sourcing_record_skus')
    }
  },
  methods: {
    // 获取区域数据
    async queryAreaList () {
      const res = await queryAreasService({})
      if (res) {
        const areaList = res.data.map(item => ({
          value: item.areaId,
          label: item.areaName,
        }));
        this.formItems = initFormItems(areaList)
      }
      this.showSearchForm = true
    },
    // 格式化入参
    initSearchParams () {
      const { bidingDate, inquiryDate, validityDate } = this.formData;
      const [bidingStartDate = '', bidingEndtDate = ''] = bidingDate || []
      const [inquiryStartDate = '', inquiryEndDate = ''] = inquiryDate || []
      const [priceExpireTimeStart = '', priceExpireTimeEnd = ''] = validityDate || []
      return _.omit({
        ...this.formData,
        bidingStartDate,
        bidingEndtDate,
        inquiryStartDate,
        inquiryEndDate,
        priceExpireTimeStart,
        priceExpireTimeEnd,
      }, 'bidingDate', 'inquiryDate')
    },
    // 采购询价列表
    async getTableData () {
      if (!this.formData.billType) {
        return this.$message.warning('请选择询价类型')
      }
      const res = await queryBidRecordList(
        { ...this.initSearchParams(), ...this.pagination },
        state => { this.tableInfo.loading = state }
      )
      if (res) {
        const { records, total } = res.data;
        this.tableInfo.list = records;
        this.total = total;
      }
    },
    // 导出
    async exportData () {
      const res = await exportOrderRecordService(
        this.initSearchParams(),
        state => { this.exportLoading = state }
      )
      if (res) {
        createExcelByExport({ excelTitle: '报价定标记录', res });
      }
    },
  }
};
</script>
